package com.puyuba.puyuba.view.fragment;

import android.annotation.SuppressLint;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;

import com.puyuba.puyuba.R;
import com.puyuba.puyuba.SkinApplication;
import com.puyuba.puyuba.view.MaxHeightScrollView;
import com.puyuba.puyuba.view.activity.MainWebActivity;
import com.pyb.java.network.NetBaseApi;
import com.puyuba.puyuba.view.activity.OneClickLoginActivity;
import com.pyb.java.cache.SPHelper;


/**
 * 服务协议页面
 **/
public class ServiceAgreementDialogFragment extends DialogFragment implements View.OnClickListener {

    private View view;
    private TextView agree;
    private TextView disAgree;
    private TextView tkServiceContent;
    private MaxHeightScrollView tkServiceSv;
    private TextView tkServiceAgain;
    private ConstraintLayout serviceAgreementCl1;
    private ConstraintLayout serviceAgreementCl2;
    private TextView tkServiceAgree2;
    private TextView tkServiceDisagree2;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.service_agreement_dialog, container);
        getDialog().setCanceledOnTouchOutside(false);//设置点击外部不消失
        //设置点击返回键不消失
        this.getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface arg0, int keyCode, KeyEvent arg2) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }
        });
        initView();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
//        getDialog().getWindow().setLayout(-1,-2 );
        getDialog().getWindow().setLayout(getScreenWidth()* 6/7,-2);
    }

    public static int getScreenWidth() {
        @SuppressLint("WrongConstant") WindowManager wm = (WindowManager) SkinApplication.getInstance().getSystemService("window");
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        return width;
    }

    private void initView() {
        /**
         * 协议内容view
         */
        serviceAgreementCl1 = view.findViewById(R.id.service_agreement_cl1);
        agree = view.findViewById(R.id.tk_service_agree);
        disAgree = view.findViewById(R.id.tk_service_disagree);
        tkServiceSv = view.findViewById(R.id.tk_service_sv);
        tkServiceContent = view.findViewById(R.id.tk_service_content);
        MaxHeightScrollView.mMaxHeight = getWindowHeight() / 7 * 4;
        agree.setOnClickListener(this);
        disAgree.setOnClickListener(this);
        /**
         * 重新阅读view
         */
        serviceAgreementCl2 = view.findViewById(R.id.service_agreement_cl2);
        tkServiceAgain = view.findViewById(R.id.tk_service_again);
        tkServiceAgree2 = view.findViewById(R.id.tk_service_agree2);
        tkServiceDisagree2 = view.findViewById(R.id.tk_service_disagree2);
        tkServiceAgree2.setOnClickListener(this);
        tkServiceDisagree2.setOnClickListener(this);

//        ViewGroup.LayoutParams layoutParams = serviceAgreementCl1.getLayoutParams();
//        layoutParams.width = ScreenScale.getScreenWidth()*5/7;
//        serviceAgreementCl1.setLayoutParams(layoutParams);
//

        initData();
    }

    /**
     * 获取屏幕高度为服务协议适配
     *
     * @return 屏幕高度
     */
    private int getWindowHeight() {
        DisplayMetrics outMetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getRealMetrics(outMetrics);
        int widthPixel = outMetrics.widthPixels;
        int heightPixel = outMetrics.heightPixels;
        return heightPixel;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tk_service_agree) {
            SPHelper.putBoolean("agree",true);
            dismiss();
            ((OneClickLoginActivity)getActivity()).initData();

        } else if (v.getId() == R.id.tk_service_disagree) {
            serviceAgreementCl1.setVisibility(View.GONE);
            serviceAgreementCl2.setVisibility(View.VISIBLE);
        }else if (v.getId()==R.id.tk_service_agree2){
            serviceAgreementCl2.setVisibility(View.GONE);
            serviceAgreementCl1.setVisibility(View.VISIBLE);
        }else if (v.getId()==R.id.tk_service_disagree2){
            //SharePreferenceUtil.putBoolean(getActivity(), "agree", true);
            dismiss();
            getActivity().finish();
        }
    }

    private void initData() {
        SpannableString spannableString = new SpannableString("3、你可阅读《用户注册协议》和《隐私政策》详细信息。如你同意，请点击“同意”开始接受我们的服务。");
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View view) {
                MainWebActivity.startMainWebActivity(getContext(), NetBaseApi.REGISETPROTOCOLURL,"用户注册协议");
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setUnderlineText(false);//去掉下划线
            }
        };
        spannableString.setSpan(clickableSpan, 6, 14, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(ContextCompat.getColor(getActivity(),R.color.color_3ca8d9));
        spannableString.setSpan(colorSpan, 6, 14, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

        ClickableSpan clickableSpan2 = new ClickableSpan() {
            @Override
            public void onClick(View view) {
                MainWebActivity.startMainWebActivity(getContext(), NetBaseApi.PRIVACYPOLICY,"隐私政策");
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setUnderlineText(false);//去掉下划线
            }

        };
        spannableString.setSpan(clickableSpan2, 15, 21, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        ForegroundColorSpan colorSpan2 = new ForegroundColorSpan(ContextCompat.getColor(getActivity(), R.color.color_3ca8d9));
        spannableString.setSpan(colorSpan2, 15, 21, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

        tkServiceContent.setText(spannableString);
        tkServiceContent.setMovementMethod(LinkMovementMethod.getInstance());
    }

}
